package com.ly.hotel.spainterface.jxd.utils;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.ly.hotel.model.JxdGuestInfoBean;
import com.ly.hotel.spainterface.jxd.bean.JxdGuestCardBean;
import com.ly.hotel.spainterface.jxd.bean.JxdOperatorCardBean;
import com.ly.hotel.spainterface.jxd.bean.Massage;
import com.ly.hotel.spainterface.jxd.bean.PluginBean;
import com.ly.hotel.spainterface.jxd.bean.Therapy;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

/**
 * Created by ThinkPad on 2018/3/20.
 */

public class XmlParseUtils {

    private static XmlParseUtils xmlParseUtils;
    private DocumentBuilder builder = null;

    public static XmlParseUtils getInstance(){
        if(xmlParseUtils == null){
            xmlParseUtils = new XmlParseUtils();
        }
        return xmlParseUtils;
    }

    private XmlParseUtils(){
        DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
        try {
            builder = domfac.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
    }

    // 解析插件配置
    public PluginBean parsePluginConfig(Context context,String pluginConfig,String pluginVersion){
        PluginBean pluginBean = null;
        try {
            Document doc = builder.parse(new ByteArrayInputStream(pluginConfig.getBytes("utf-8")));
            NodeList pluginVersionNode = doc.getElementsByTagName(pluginVersion);// 获得插件版本号下的节点
            pluginBean = new PluginBean();
            for(int i = 0;i < pluginVersionNode.getLength();i ++){
                NodeList jishiNode = ((Element) pluginVersionNode.item(i)).getElementsByTagName("jishi");// 获得xiaofei下的节点
                if(jishiNode.getLength() >= 1){
                    // 解析ip节点
                    NodeList headNode = ((Element) jishiNode.item(0)).getElementsByTagName("ip");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String ip = headNode.item(0).getFirstChild().getNodeValue().trim();
                            pluginBean.setIp(ip);
                        } catch (Exception e) {
                            Toast.makeText(context, "ip配置有误！", Toast.LENGTH_SHORT).show();
                            pluginBean.setPort("");
                        }
                    }
                    // 解析port节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("port");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String port = headNode.item(0).getFirstChild().getNodeValue().trim();
                            pluginBean.setPort(port);
                        } catch (Exception e) {
                            Toast.makeText(context, "port配置有误！", Toast.LENGTH_SHORT).show();
                            pluginBean.setPort("");
                        }
                    }

                    // 解析port节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("arrange");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String arrange = headNode.item(0).getFirstChild().getNodeValue().trim();
                            int arrangeNum = Integer.parseInt(arrange);
                            pluginBean.setArrange(arrangeNum);
                        } catch (Exception e) {
                            //Toast.makeText(context, "arrange配置有误！", Toast.LENGTH_SHORT).show();
                        }
                    }

                    // 解析itemclass1节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("itemclass1");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String itemclass1 = headNode.item(0).getFirstChild().getNodeValue().trim();
                            //Log.i("JxdJSActivity","itemclass1=" + itemclass1);
                            pluginBean.setItemclass1(itemclass1);
                        } catch (Exception e) {
                            pluginBean.setItemclass1("");
                        }
                    }

                    // 解析itemclass2节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("itemclass2");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String itemclass2 = headNode.item(0).getFirstChild().getNodeValue().trim();
                            //Log.i("JxdJSActivity","itemclass2=" + itemclass2);
                            pluginBean.setItemclass2(itemclass2);
                        } catch (Exception e) {
                            pluginBean.setItemclass2("");
                        }
                    }

                    // 解析waiterId节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("waiterId");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String waiterId = headNode.item(0).getFirstChild().getNodeValue().trim();
                            boolean isSwipeWaiterId = Boolean.parseBoolean(waiterId);
                            pluginBean.setWaiterId(isSwipeWaiterId);
                        } catch (Exception e) {

                        }
                    }

                    // 解析cardtype节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("cardtype");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String cardtype = headNode.item(0).getFirstChild().getNodeValue().trim();
                            pluginBean.setCardtype(cardtype);
                        } catch (Exception e) {
                            pluginBean.setCardtype("");
                        }
                    }

                    // 解析serveDel节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("serveDel");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String serveDel = headNode.item(0).getFirstChild().getNodeValue().trim();
                            //Log.i("JxdJSActivity","serveDel=" + serveDel);
                            pluginBean.setServeDel(serveDel);
                        } catch (Exception e) {
                            pluginBean.setServeDel("");
                        }
                    }

                    // 解析recommend节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("recommend");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String recommend = headNode.item(0).getFirstChild().getNodeValue().trim();
                            //Log.i("JxdJSActivity","recommend=" + recommend);
                            pluginBean.setRecommend(recommend);
                        } catch (Exception e) {
                            pluginBean.setRecommend("");
                        }
                    }

                    // 解析serveDel节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("serveSort");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String serveSort = headNode.item(0).getFirstChild().getNodeValue().trim();
                            //Log.i("JxdJSActivity","serveSort=" + serveSort);
                            pluginBean.setServeSort(serveSort);
                        } catch (Exception e) {
                            pluginBean.setServeSort("");
                        }
                    }

                    // 解析canArrange节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("canArrange");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String canArrange = headNode.item(0).getFirstChild().getNodeValue().trim();
                            //Log.i("JxdJSActivity","canArrange=" + canArrange);
                            pluginBean.setCanArrange(canArrange);
                        } catch (Exception e) {
                            pluginBean.setCanArrange("");
                        }
                    }

                    // 解析arrangeTimeOut节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("arrangeTimeOut");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String arrangeTimeOut = headNode.item(0).getFirstChild().getNodeValue().trim();
                            //Log.i("JxdJSActivity","arrangeTimeOut=" + arrangeTimeOut);
                            pluginBean.setArrangeTimeOut(arrangeTimeOut);
                        } catch (Exception e) {
                            pluginBean.setArrangeTimeOut("");
                        }
                    }

                    // 解析artNumSort节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("artNumSort");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String artNumSort = headNode.item(0).getFirstChild().getNodeValue().trim();
                            boolean isArtNumSort = Boolean.parseBoolean(artNumSort);
                            //Log.i("JxdJSActivity","isArtNumSort=" + isArtNumSort);
                            pluginBean.setArtNumSort(isArtNumSort);
                        } catch (Exception e) {

                        }
                    }

                    // 解析showName节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("showName");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String showName = headNode.item(0).getFirstChild().getNodeValue().trim();
                            boolean isShowName = Boolean.parseBoolean(showName);
                            pluginBean.setShowName(isShowName);
                        } catch (Exception e) {

                        }
                    }

                    // 解析nameFiltrate节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("nameFiltrate");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String nameFiltrate = headNode.item(0).getFirstChild().getNodeValue().trim();
                            boolean isNameFiltrate = Boolean.parseBoolean(nameFiltrate);
                            pluginBean.setNameFiltrate(isNameFiltrate);
                        } catch (Exception e) {

                        }
                    }

                    // 解析showEndTime节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("showEndTime");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String showEndTime = headNode.item(0).getFirstChild().getNodeValue().trim();
                            boolean isShowEndTime = Boolean.parseBoolean(showEndTime);
                            pluginBean.setShowEndTime(isShowEndTime);
                        } catch (Exception e) {

                        }
                    }

                    // 解析imgSrc节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("imgSrc");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String imgSrc = headNode.item(0).getFirstChild().getNodeValue().trim();
                            pluginBean.setImgSrc(imgSrc);
                        } catch (Exception e) {

                        }
                    }
                }
            }
            return pluginBean;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pluginBean;
    }

    // 解析做钟项目数据
    public List<Massage> parseMassageItem(String hallItemResult){
        List<Massage> massages = new ArrayList<>();
        try {
            Document doc = builder.parse(new ByteArrayInputStream(hallItemResult.getBytes("utf-8")));
            NodeList gsNode = doc.getElementsByTagName("gs");
            if(gsNode.getLength() >= 1){
                //Log.i("JxdJSActivity","gs节点长度:" + gsNode.getLength());
                NodeList rowNode = ((Element) gsNode.item(0)).getElementsByTagName("row");
                if(rowNode.getLength() >= 1){
                    //Log.i("JxdJSActivity","row节点长度:" + rowNode.getLength());
                    Massage massage = null;
                    for(int i = 0;i < rowNode.getLength();i ++){
                        massage = new Massage();
                        NodeList massagetypenoNode = ((Element) rowNode.item(i)).getElementsByTagName("massagetypeno");
                        if(massagetypenoNode.getLength() >= 1 && massagetypenoNode.item(0) != null){
                            try {
                                String massagetypeno = massagetypenoNode.item(0).getFirstChild().getNodeValue().trim();
                                massage.setMassagetypeno(massagetypeno);
                                //Log.i("JxdJSActivity","massagetypeno:" + massagetypeno);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }
                        NodeList massagenameNode = ((Element) rowNode.item(i)).getElementsByTagName("massagename");
                        if(massagenameNode.getLength() >= 1 && massagenameNode.item(0) != null){
                            try{
                                String massagename = massagenameNode.item(0).getFirstChild().getNodeValue().trim();
                                massage.setMassagename(massagename);
                                //Log.i("JxdJSActivity","massagename:" + massagename);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }
                        NodeList ishallNode = ((Element) rowNode.item(i)).getElementsByTagName("ishall");
                        if(ishallNode.getLength() >= 1 && ishallNode.item(0) != null){
                            try{
                                String ishall = ishallNode.item(0).getFirstChild().getNodeValue().trim();
                                massage.setIshall(ishall);
                                //Log.i("JxdJSActivity","ishall:" + ishall);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }
                        massages.add(massage);
                    }
                }
            }
        } catch (Exception e) {
            Log.i("JxdJSActivity","无法解析文档！");
            //e.printStackTrace();
        }
        return massages;
    }

    // 解析操作员卡
    public JxdOperatorCardBean parseOperatorCard(String operatorDataResult){
        JxdOperatorCardBean operatorCardBean = null;
        try {
            Document doc = builder.parse(new ByteArrayInputStream(operatorDataResult.getBytes("utf-8")));
            NodeList gsNode = doc.getElementsByTagName("gs");
            operatorCardBean = new JxdOperatorCardBean();
            if(gsNode.getLength() >= 1){
                NodeList cardidNode= ((Element) gsNode.item(0)).getElementsByTagName("cardid");
                if(cardidNode.getLength() >= 1 && cardidNode.item(0) != null){
                    try {
                        String cardid = cardidNode.item(0).getFirstChild().getNodeValue().trim();
                        operatorCardBean.setCardid(cardid);
                        //Log.i("JxdJSActivity","cardid:" + cardid);
                    }catch (Exception e){
                        //e.printStackTrace();
                    }
                }

                NodeList opernoNode= ((Element) gsNode.item(0)).getElementsByTagName("operno");
                if(opernoNode.getLength() >= 1 && opernoNode.item(0) != null){
                    try {
                        String operno = opernoNode.item(0).getFirstChild().getNodeValue().trim();
                        operatorCardBean.setOperno(operno);
                        //Log.i("JxdJSActivity","operno:" + operno);
                    }catch (Exception e){
                        //e.printStackTrace();
                    }
                }

                NodeList opernameNode= ((Element) gsNode.item(0)).getElementsByTagName("opername");
                if(opernameNode.getLength() >= 1 && opernameNode.item(0) != null){
                    try {
                        String opername = opernameNode.item(0).getFirstChild().getNodeValue().trim();
                        operatorCardBean.setOpername(opername);
                        //Log.i("JxdJSActivity","opername:" + opername);
                    }catch (Exception e){
                        //e.printStackTrace();
                    }
                }
            }
        }catch (Exception e){
            //e.printStackTrace();
        }
        return operatorCardBean;
    }

    // 解析客人手牌
    public JxdGuestCardBean parseGuestCard(String guestDataResut){
        JxdGuestCardBean cardBean = null;
        try{
            Document doc = builder.parse(new ByteArrayInputStream(guestDataResut.getBytes("utf-8")));
            NodeList gsNode = doc.getElementsByTagName("gs");
            cardBean = new JxdGuestCardBean();
            if(gsNode.getLength() >= 1){
                NodeList itemNode = ((Element) gsNode.item(0)).getElementsByTagName("Item");
                if(itemNode.getLength() >= 1){
                    NodeList artflagNode = ((Element) itemNode.item(0)).getElementsByTagName("artflag");
                    if(artflagNode.getLength() >= 1 && artflagNode.item(0) != null){
                        try {
                            String artflag = artflagNode.item(0).getFirstChild().getNodeValue().trim();
                            cardBean.setArtflag(artflag);
                            //Log.i("JxdJSActivity","artflag:" + artflag);
                        }catch (Exception e){
                            //e.printStackTrace();
                        }
                    }

                    NodeList rfcardnoNode = ((Element) itemNode.item(0)).getElementsByTagName("rfcardno");
                    if(rfcardnoNode.getLength() >= 1 && rfcardnoNode.item(0) != null){
                        try {
                            String rfcardno = rfcardnoNode.item(0).getFirstChild().getNodeValue().trim();
                            cardBean.setRfcardno(rfcardno);
                            //Log.i("JxdJSActivity","rfcardno:" + rfcardno);
                        }catch (Exception e){
                            //e.printStackTrace();
                        }
                    }

                    NodeList keynoNode = ((Element) itemNode.item(0)).getElementsByTagName("keyno");
                    if(keynoNode.getLength() >= 1 && keynoNode.item(0) != null){
                        try {
                            String keyno = keynoNode.item(0).getFirstChild().getNodeValue().trim();
                            cardBean.setKeyno(keyno);
                            //Log.i("JxdJSActivity","keyno:" + keyno);
                        }catch (Exception e){
                            //e.printStackTrace();
                        }
                    }
                }
            }
        }catch (Exception e){
            //e.printStackTrace();
            Log.i("JxdJSActivity","无法解析文档！");
        }
        return cardBean;
    }

    // 解析技师资料数据
    public List<Therapy> parseTherapy(String therapyDataResult){
        List<Therapy> therapies = new ArrayList<>();
        try {
            Document doc = builder.parse(new ByteArrayInputStream(therapyDataResult.getBytes("utf-8")));
            NodeList gsNode = doc.getElementsByTagName("gs");
            if(gsNode.getLength() >= 1){
                NodeList artNode = ((Element) gsNode.item(0)).getElementsByTagName("art");
                if(artNode.getLength() >= 1){
                    Therapy therapy = null;
                    for(int i = 0;i < artNode.getLength();i ++){
                        therapy = new Therapy();
                        NodeList artnoNode = ((Element) artNode.item(i)).getElementsByTagName("artno");
                        if(artnoNode.getLength() >= 1 && artnoNode.item(0) != null){
                            try {
                                String artno = artnoNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setArtNo(artno);
                                //Log.i("JxdJSActivity","artno:" + artno);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList cnameNode = ((Element) artNode.item(i)).getElementsByTagName("cname");
                        if(cnameNode.getLength() >= 1 && cnameNode.item(0) != null){
                            try {
                                String cname = cnameNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setArtName(cname);
                                //Log.i("JxdJSActivity","cname:" + cname);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList sexNode = ((Element) artNode.item(i)).getElementsByTagName("sex");
                        if(sexNode.getLength() >= 1 && sexNode.item(0) != null){
                            try {
                                String sex = sexNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setSex(sex);
                                //Log.i("JxdJSActivity","sex:" + sex);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList ondutyNode = ((Element) artNode.item(i)).getElementsByTagName("onduty");
                        if(ondutyNode.getLength() >= 1 && ondutyNode.item(0) != null){
                            try {
                                String onduty = ondutyNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setOnduty(onduty);
                                //Log.i("JxdJSActivity","onduty:" + onduty);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList statusNode = ((Element) artNode.item(i)).getElementsByTagName("status");
                        if(statusNode.getLength() >= 1 && statusNode.item(0) != null){
                            try {
                                String status = statusNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setStatus(status);
                                //Log.i("JxdJSActivity","status:" + status);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList downminuteNode = ((Element) artNode.item(i)).getElementsByTagName("downminute");
                        if(downminuteNode.getLength() >= 1 && downminuteNode.item(0) != null){
                            try {
                                String downminute = downminuteNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setDownMinute(downminute);
                                //Log.i("JxdJSActivity","downminute:" + downminute);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList ageNode = ((Element) artNode.item(i)).getElementsByTagName("age");
                        if(ageNode.getLength() >= 1 && ageNode.item(0) != null){
                            try {
                                String age = ageNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setAge(age);
                                //Log.i("JxdJSActivity","age:" + age);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList heightNode = ((Element) artNode.item(i)).getElementsByTagName("height");
                        if(heightNode.getLength() >= 1 && heightNode.item(0) != null){
                            try {
                                String height = heightNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setHeight(height);
                                //Log.i("JxdJSActivity","height:" + height);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList weightNode = ((Element) artNode.item(i)).getElementsByTagName("weight");
                        if(weightNode.getLength() >= 1 && weightNode.item(0) != null){
                            try {
                                String weight = weightNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setWeight(weight);
                                //Log.i("JxdJSActivity","height:" + weight);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList homeNode = ((Element) artNode.item(i)).getElementsByTagName("home");
                        if(homeNode.getLength() >= 1 && homeNode.item(0) != null){
                            try {
                                String home = homeNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setHome(home);
                                //Log.i("JxdJSActivity","home:" + home);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList artmodenoNode = ((Element) artNode.item(i)).getElementsByTagName("artmodeno");
                        if(artmodenoNode.getLength() >= 1 && artmodenoNode.item(0) != null){
                            try {
                                String artmodeno = artmodenoNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setArtModeNo(artmodeno);
                                //Log.i("JxdJSActivity","artmodeno:" + artmodeno);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList v_memoNode = ((Element) artNode.item(i)).getElementsByTagName("v_memo");
                        if(v_memoNode.getLength() >= 1 && v_memoNode.item(0) != null){
                            try {
                                String vMemo = v_memoNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setvMemo(vMemo);
                                //Log.i("JxdJSActivity","vMemo:" + vMemo);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList i_orderNode = ((Element) artNode.item(i)).getElementsByTagName("i_order");
                        if(i_orderNode.getLength() >= 1 && i_orderNode.item(0) != null){
                            try {
                                String iOrder = i_orderNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setiOrder(iOrder);
                                //Log.i("JxdJSActivity","iOrder:" + iOrder);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList showpicNode = ((Element) artNode.item(i)).getElementsByTagName("showpic");
                        if(showpicNode.getLength() >= 1 && showpicNode.item(0) != null){
                            try {
                                String showPic = showpicNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setShowPic(showPic);
                                //Log.i("JxdJSActivity","showPic:" + showPic);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        therapies.add(therapy);
                    }
                }
            }
        }catch (Exception e){
            Log.i("JxdJSActivity","无法解析文档！");
            //e.printStackTrace();
        }
        return therapies;
    }

    // 解析推荐技师数据技师资料数据
    public List<Therapy> parseRecommendTherapyData(String therapyDataResult){
        List<Therapy> therapies = new ArrayList<>();
        try {
            Document doc = builder.parse(new ByteArrayInputStream(therapyDataResult.getBytes("utf-8")));
            NodeList gsNode = doc.getElementsByTagName("gs");
            if(gsNode.getLength() >= 1){
                NodeList artNode = ((Element) gsNode.item(0)).getElementsByTagName("art");
                if(artNode.getLength() >= 1){
                    Therapy therapy = null;
                    for(int i = 0;i < artNode.getLength();i ++){
                        therapy = new Therapy();
                        NodeList artnoNode = ((Element) artNode.item(i)).getElementsByTagName("artno");
                        if(artnoNode.getLength() >= 1 && artnoNode.item(0) != null){
                            try {
                                String artno = artnoNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setArtNo(artno);
                                //Log.i("JxdJSActivity","artno:" + artno);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList cnameNode = ((Element) artNode.item(i)).getElementsByTagName("cname");
                        if(cnameNode.getLength() >= 1 && cnameNode.item(0) != null){
                            try {
                                String cname = cnameNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setArtName(cname);
                                //Log.i("JxdJSActivity","cname:" + cname);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList sexNode = ((Element) artNode.item(i)).getElementsByTagName("sex");
                        if(sexNode.getLength() >= 1 && sexNode.item(0) != null){
                            try {
                                String sex = sexNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setSex(sex);
                                //Log.i("JxdJSActivity","sex:" + sex);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList ondutyNode = ((Element) artNode.item(i)).getElementsByTagName("onduty");
                        if(ondutyNode.getLength() >= 1 && ondutyNode.item(0) != null){
                            try {
                                String onduty = ondutyNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setOnduty(onduty);
                                //Log.i("JxdJSActivity","onduty:" + onduty);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList statusNode = ((Element) artNode.item(i)).getElementsByTagName("status");
                        if(statusNode.getLength() >= 1 && statusNode.item(0) != null){
                            try {
                                String status = statusNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setStatus(status);
                                //Log.i("JxdJSActivity","status:" + status);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList downminuteNode = ((Element) artNode.item(i)).getElementsByTagName("downminute");
                        if(downminuteNode.getLength() >= 1 && downminuteNode.item(0) != null){
                            try {
                                String downminute = downminuteNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setDownMinute(downminute);
                                //Log.i("JxdJSActivity","downminute:" + downminute);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList ageNode = ((Element) artNode.item(i)).getElementsByTagName("age");
                        if(ageNode.getLength() >= 1 && ageNode.item(0) != null){
                            try {
                                String age = ageNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setAge(age);
                                //Log.i("JxdJSActivity","age:" + age);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList heightNode = ((Element) artNode.item(i)).getElementsByTagName("height");
                        if(heightNode.getLength() >= 1 && heightNode.item(0) != null){
                            try {
                                String height = heightNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setHeight(height);
                                //Log.i("JxdJSActivity","height:" + height);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList weightNode = ((Element) artNode.item(i)).getElementsByTagName("weight");
                        if(weightNode.getLength() >= 1 && weightNode.item(0) != null){
                            try {
                                String weight = weightNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setWeight(weight);
                                //Log.i("JxdJSActivity","height:" + weight);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList homeNode = ((Element) artNode.item(i)).getElementsByTagName("home");
                        if(homeNode.getLength() >= 1 && homeNode.item(0) != null){
                            try {
                                String home = homeNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setHome(home);
                                //Log.i("JxdJSActivity","home:" + home);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList artmodenoNode = ((Element) artNode.item(i)).getElementsByTagName("artmodeno");
                        if(artmodenoNode.getLength() >= 1 && artmodenoNode.item(0) != null){
                            try {
                                String artmodeno = artmodenoNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setArtModeNo(artmodeno);
                                //Log.i("JxdJSActivity","artmodeno:" + artmodeno);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList v_memoNode = ((Element) artNode.item(i)).getElementsByTagName("v_memo");
                        if(v_memoNode.getLength() >= 1 && v_memoNode.item(0) != null){
                            try {
                                String vMemo = v_memoNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setvMemo(vMemo);
                                //Log.i("JxdJSActivity","vMemo:" + vMemo);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList i_orderNode = ((Element) artNode.item(i)).getElementsByTagName("i_order");
                        if(i_orderNode.getLength() >= 1 && i_orderNode.item(0) != null){
                            try {
                                String iOrder = i_orderNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setiOrder(iOrder);
                                //Log.i("JxdJSActivity","iOrder:" + iOrder);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        NodeList showpicNode = ((Element) artNode.item(i)).getElementsByTagName("showpic");
                        if(showpicNode.getLength() >= 1 && showpicNode.item(0) != null){
                            try {
                                String showPic = showpicNode.item(0).getFirstChild().getNodeValue().trim();
                                therapy.setShowPic(showPic);
                                //Log.i("JxdJSActivity","showPic:" + showPic);
                            }catch (Exception e){
                                //e.printStackTrace();
                            }
                        }

                        therapies.add(therapy);
                    }
                }
            }
        }catch (Exception e){
            Log.i("JxdJSActivity","无法解析文档！");
            //e.printStackTrace();
        }
        return therapies;
    }

    //解析PMS 入住客人信息
    public JxdGuestInfoBean parseWelcomeWord(String welcomeWordResult){
        JxdGuestInfoBean welcomeWordBean = null;
        try {
            Document doc = builder.parse(new ByteArrayInputStream(welcomeWordResult.getBytes("utf-8")));
            welcomeWordBean = new JxdGuestInfoBean();
            //解析客人基础信息返回
            NodeList guestInfoResultNode = doc.getElementsByTagName("QueryGuestInfoResult");
            if (guestInfoResultNode.getLength() >= 1){

                //解析 Result
                NodeList heaNode = ((Element) guestInfoResultNode.item(0)).getElementsByTagName("Result");
                if (heaNode.getLength() >= 1 && heaNode.item(0) != null){
                    try {
                        int result = Integer.parseInt(heaNode.item(0).getFirstChild().getNodeValue().trim());
                        welcomeWordBean.setResult(result);
                    } catch (Exception e) {

                    }
                }

                //解析 ErrorInfo
                heaNode = ((Element) guestInfoResultNode.item(0)).getElementsByTagName("ErrorInfo");
                if (heaNode.getLength() >= 1 && heaNode.item(0) != null){
                    try {
                        String ErrorInfo = heaNode.item(0).getFirstChild().getNodeValue().trim();
                        welcomeWordBean.setErrorInfo(ErrorInfo);
                    } catch (Exception e) {

                    }
                }

                //成功了才解析下面的字段
                if (welcomeWordBean.getResult() == 200){

                    //解析 GuestRecords
                    heaNode = ((Element) guestInfoResultNode.item(0)).getElementsByTagName("GuestRecords");
                    if (heaNode.getLength() >= 1 && heaNode.item(0) != null){
                        try {
                            String GuestRecords = heaNode.item(0).getFirstChild().getNodeValue().trim();
                            welcomeWordBean.setGuestRecords(GuestRecords);
                        } catch (Exception e) {

                        }
                    }

                    if (welcomeWordBean.getGuestRecords() != null){
                        doc = builder.parse(new ByteArrayInputStream(welcomeWordBean.getGuestRecords().getBytes("utf-8")));
                        NodeList guestItemNode = doc.getElementsByTagName("GuestItem");

                        if (guestItemNode.getLength() >= 1){

                            //解析 客人姓名 GuestName
                            heaNode = ((Element) guestItemNode.item(0)).getElementsByTagName("GuestName");
                            if (heaNode.getLength() >= 1 && heaNode.item(0) != null){
                                try {
                                    String GuestName = heaNode.item(0).getFirstChild().getNodeValue().trim();
                                    welcomeWordBean.setGuestName(GuestName);
                                } catch (Exception e) {

                                }
                            }

                            //解析 性别 Gender
                            heaNode = ((Element) guestItemNode.item(0)).getElementsByTagName("Gender");
                            if (heaNode.getLength() >= 1 && heaNode.item(0) != null){
                                try {
                                    String Gender = heaNode.item(0).getFirstChild().getNodeValue().trim();
                                    welcomeWordBean.setGender(Gender);
                                } catch (Exception e) {

                                }
                            }

                            //解析 团号 GroupNo
                            heaNode = ((Element) guestItemNode.item(0)).getElementsByTagName("GroupNo");
                            if (heaNode.getLength() >= 1 && heaNode.item(0) != null){
                                try {
                                    String GroupNo = heaNode.item(0).getFirstChild().getNodeValue().trim();
                                    welcomeWordBean.setGroupNo(GroupNo);
                                } catch (Exception e) {

                                }
                            }

                            //解析 团名 GroupName
                            heaNode = ((Element) guestItemNode.item(0)).getElementsByTagName("GroupName");
                            if (heaNode.getLength() >= 1 && heaNode.item(0) != null){
                                try {
                                    String GroupName = heaNode.item(0).getFirstChild().getNodeValue().trim();
                                    welcomeWordBean.setGroupName(GroupName);
                                } catch (Exception e) {

                                }
                            }
                        }
                    }


                }else{
                    Log.e("welcome", "错误信息：" + welcomeWordBean.getErrorInfo());
                }
            }
        } catch (Exception e) {
            Log.i("welcome","解析xml失败");
//            e.printStackTrace();
        }

        return welcomeWordBean;
    }
}
